/* 
 * 
 * Copyright 2011 The University of Adelaide
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 *  
 */

package au.org.aekos.ands.bdbsa.process;

import au.org.aekos.ands.bdbsa.data.Luprojectbasis;
import au.org.aekos.ands.global.Schema;
import au.org.aekos.ands.global.Schema.Attribute;
import au.org.aekos.ands.global.Util;
import au.org.ands.standards.rifCs.registryObjects.ActivityDocument.Activity;
import au.org.ands.standards.rifCs.registryObjects.NameType;
import au.org.ands.standards.rifCs.registryObjects.RegistryObjectsDocument.RegistryObjects.RegistryObject;

/**
 * The activity generator for BDBSA.
 */
public class GenerateActivity {
	private Schema schema;
	
	/**
	 * Instantiates a new activity generator.
	 *
	 * @param schema the schema through which the activity is defined.
	 */
	public GenerateActivity(Schema schema) { this.schema = schema; }
	
	/**
	 * Generates the activity with the given identifier.
	 *
	 * @param basis the project basis for which an activity is to be generated.
	 * @return a new registry object containing the activity.
	 */
	public RegistryObject generateActivity(Luprojectbasis basis) {
		if (basis == null) { return null; }
		
		String dbActivityCode = basis.getBasiscode();
		String dbActivityName = basis.getBasisdesc();
		
		RegistryObject object = schema.generateActivity(dbActivityCode);
		Activity activity = object.getActivity();

		NameType[] names = activity.getNameArray();

		NameType primary = Util.getName(names, Attribute.PRIMARY);
		if (primary == null) {
            activity.addNewName().set(Schema.generateName(Attribute.PRIMARY, dbActivityName));
		}

		NameType abbreviated = Util.getName(names, Attribute.ABBREVIATED);
		if (abbreviated == null) {
            activity.addNewName().set(Schema.generateName(Attribute.ABBREVIATED, "BDBSA:" + dbActivityCode));			
		}
		
		return object;
	}
}
